#include "config.h"
config::config(QObject *parent) :QObject(parent)
{

}
void config::sauvegarde_config(QString url_xml, QString url_post, QString cookie, QString post_scheme, int number)
{
    tribunenumber.setNum(number);
    conf_file.setFileName("testapp.cnf");
    //Construction du fichier de configuration
    //img_file_cnf.append();
    conf_file.open(QFile::WriteOnly|QFile::Text);
    QXmlStreamWriter out_xml_cnf(&conf_file);
    out_xml_cnf.setAutoFormatting(true);
    out_xml_cnf.writeStartDocument();
    out_xml_cnf.writeStartElement("configuration");
    out_xml_cnf.writeStartElement("tribune");
    out_xml_cnf.writeTextElement("numero",tribunenumber);
    out_xml_cnf.writeTextElement("xmlfileurl",url_xml);
    out_xml_cnf.writeTextElement("posturl",url_post);
    out_xml_cnf.writeTextElement("cookie",cookie);
    if(post_scheme=="board[message]=%s")
    {
        out_xml_cnf.writeTextElement("boardtype","1");
    }
    else
    {
        out_xml_cnf.writeTextElement("boardtype","2");
    }
    out_xml_cnf.writeEndElement();
    out_xml_cnf.writeEndDocument();
    //Fermuture du fichier all good
    conf_file.close();
}
void config::lecture_config(void)
{
    bool ok=0;
    //Utilisation de qsettings
    company="musselsGMBH";
    application_name="qtcoincoin";
    QSettings settings(company,application_name);
    //Envoi vers la fonction de config
    //On passe par une QStringList que l'on transmet à la fenetre de configaration
    int nb_board=settings.value("BoardConfig/Board number").toInt(&ok);
    emit send_board_add(settings.value("BoardConfig/Board server").toString(),nb_board);
    emit send_board_message_fl(settings.value("BoardConfig/Message type").toString());
    emit send_board_filexml_path(settings.value("BoardConfig/Board xml file").toString());
    emit send_board_post_path(settings.value("BoardConfig/Board post").toString());

}
void config::write_conf_board_init(void)
{
    QString trib_num;
    int number=1;
    trib_num.setNum(number,10);
    company="World Company Incorporated";
    application_name="qtcoincoin";
    QSettings::setDefaultFormat(QSettings::IniFormat);
    QSettings settings(company,application_name);
    settings.beginGroup("BoardConfig");
    settings.setValue("Board_number",trib_num);
    settings.setValue("Board_server",test_add);
    settings.setValue("Board_xml_file",test_xml_path);
    settings.setValue("Board_post",test_post_path);
    settings.setValue("Cookie",test_cookie);
    settings.setValue("Message_type",test_post_scheme_cn);
    settings.endGroup();

}
void config::read_conf()
{
    bool ok=false;
    company="musselsGMBH";
    application_name="qtcoincoin";
    QSettings settings(company,application_name);
    /*Permet de savoir le numéro de la tribune */
    tribunenumber=settings.value("BoardConfig/Board number").toString().toInt(&ok,10);
    /*Envoi les parametres vers la config pour l'utilisation avec l'assistant de config*/
    emit send_parameters_to_cnf(settings.value("BoardConfig/Board xml file").toString(),settings.value("BoardConfig/Board post").toString(),settings.value("BoardConfig/Cookie").toString(),settings.value("BoardConfig/Message type").toString(),1);




}
void config::write_net_settings(QString add_proxy, QString proxy_port)
{
    company="World Company Incorporated";
    application_name="qtcoincoin";
    QSettings::setDefaultFormat(QSettings::IniFormat);
    QSettings settings(company,application_name);
    settings.beginGroup("Network");
    settings.setValue("proxyaddress",add_proxy);
    settings.setValue("proxyport",proxy_port);
    settings.endGroup();
}
void config::write_general_settings()
{
    company="World Company Incorporated";
    application_name="qtcoincoin";
    QSettings::setDefaultFormat(QSettings::IniFormat);
    QSettings settings(company,application_name);
    settings.beginGroup("Browser");
    settings.setValue("program_cmd",browser);
    settings.endGroup();
}

bool config::found_board_num(int numboard)
{
    QString trib_num;
    trib_num.setNum(numboard,10);
    company="musselsGMBH";
    application_name="qtcoincoin";
    QSettings settings(company,application_name);
    return true;
}
void config::receive_data_add_from_cnf(QString data)
{
    //réception des donnés de la config
    test_add=data;
}
void config::receive_data_post_path_from_cnf(QString data)
{
    test_post_path=data;
}
void config::receive_data_xml_path_from_cnf(QString data)
{
    test_xml_path=data;
}
void config::receive_data_post_scheme_from_cnf(QString data)
{

    test_post_scheme_cn=data;
}
void config::receive_data_user_agent_from_cnf(QString data)
{
      test_user_agent=data;
}
void config::receive_data_cookie_from_cnf(QString data)
{
    test_cookie=data;
}
